#ifndef MQTTCLIENT_HEAD_H
#define MQTTCLIENT_HEAD_H
#include <iostream>
#include <mosquitto.h>
#include <string.h>
#include <errno.h>
// 条件变量
#include <condition_variable>
#include <queue>
#include "MQTTMessage.h"

using namespace std;

class MQTTClient
{
public:
    MQTTClient(const string &pubTopic, const string &subTopic, int qos);
    ~MQTTClient();
    bool connect(const string &ip, uint16_t port);
    bool disconnect(void);
    // 发布消息
    int send(const void *data, uint32_t len);
    int recv(void *data, uint32_t len);
    // 非成员函数 不需要对象调用
    // 接收到消息的回调函数
    static void message(mosquitto *, void *obj, const mosquitto_message *msg);
    void wait();
    void notify();

private:
    // 发布主题
    string pubTopic;
    // 订阅主题
    string subTopic;
    int qos;
    struct mosquitto *mos;

    std::mutex mutex;
    std::mutex qmutex;
    std::condition_variable cv;
    queue<MQTTMessage> msgQueue;
};

#endif